package test
{
	import flexunit.flexui.patterns.AssertContainedPattern;
	import flexunit.framework.TestCase;
	
	import org.as3lib.color.Color;

	public class ColorTest extends TestCase
	{
		public function testColorComponents():void {
			var color:Color = new Color(0xAABBCC);
			
			assertEquals("Red component is accurately represented.", color.red, 0xAA);
			assertEquals("Green component is accurately represented.", color.green, 0xBB);
			assertEquals("Blue component is accurately represented.", color.blue, 0xCC);
			
			color = new Color(0xFF00CC);
			assertEquals("Red percentage is correct.", color.redPercent, 1);
			assertEquals("Green percentage is correct.", color.greenPercent, 0);
			assertEquals("Blue percentage is correct.", color.bluePercent, 0xCC / 0xFF);
		}
		
		public function testHSV():void {
			var white:Color = new Color(0xFFFFFF);
			var black:Color = new Color(0x000000);
			var red:Color = new Color(0xFF0000);
			
			assertEquals("black has a 0 value", black.valuePercentage, 0);
			assertEquals("white has a 100% value", white.valuePercentage, 1);

			red.hue += 120;
			assertEquals("changing hue works", red.hex, 0x00FF00 );
			
			assertEquals("black saturation is 0", black.saturation, 0 );
			red.saturation = 0;
			assertTrue("setting saturation to 0 makes isGrey true", red.isGrey());
		}
		
		public function testInvert():void {
			var color:Color = new Color(0x336699);
			assertEquals("Getting the inverse of the color produces an opposite color.", color.getInverse().hex, 0xCC9966);
			
			color = new Color(0);
			color.invert();
			assertEquals("Inverting the color produces an opposite color.", color.hex, 0xFFFFFF);
		}						
		
		public function testConversions():void {
			var color:Color = new Color(0xAABBCC);
			
			assertTrue("Conversion to Array produces accurate results in the red channel.", color.toArray()[0] == 0xAA);
			assertTrue("Conversion to Array produces accurate results in the green channel.", color.toArray()[1] == 0xBB);
			assertTrue("Conversion to Array produces accurate results in the blue channel.", color.toArray()[2] == 0xCC);
		}
		
		public function testCMYK():void {
			var color:Color;
			
			color = new Color(0x00FFFF);
			assertEquals("cyan is correct", color.cyan, 1);
			color = new Color(0xFF00FF);
			assertEquals("magenta is correct", color.magenta, 1);
			color = new Color(0xFFFF00);
			assertEquals("yellow is correct", color.yellow, 1);
			color = new Color(0x000000);
			assertEquals("black is correct", color.black, 1);
			
			color = new Color(0xFF9900);
			var cmyk:Color = new Color();
			cmyk.setCMYK(color.cyan, color.magenta, color.yellow, color.black);
			assertEquals("Converting from CMYK to RGB is correct", color.hex, cmyk.hex);
		}
		
		public function testGetNearestWebColor():void {
			var color:Color = new Color(new Color(0xAA5566).getNearestWebColor());
			
			assertEquals("Converts accurately to the nearest web color.", color.hex, 0x996666); 
			assertTrue("Returns a valid web color.", (color.red % 0x33 == 0) && (color.green % 0x33 == 0) && (color.blue % 0x33 == 0) ); 
		}
	}
}